package com.zlyx.easy.core.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.List;

import org.apache.maven.model.Developer;
import org.apache.maven.model.Model;
import org.apache.maven.model.Scm;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;

import com.zlyx.easy.core.list.Lists;
import com.zlyx.easy.core.tool.Ops;
import com.zlyx.easy.core.utils.ApplicationUtils;

/**
 * @Auth 赵光
 * @Describle
 * @2019年6月25日 下午4:33:58
 */
public final class ProjectUtils {

	private static Model model;

	public static Model getModel() {
		if (model == null) {
			model = new Model();
			try {
				if (!handleMaven()) {
					handleGradle();
				}
				;
			} catch (Exception e) {
			}
		}
		notNullModel();
		return model;
	}

	private static void notNullModel() {
		if (Ops.isEmpty(model.getVersion())) {
			model.setVersion("0.0.1-SNAPSHOT");
		}
		if (Ops.isEmpty(model.getUrl())) {
			model.setUrl("https://gitee.com/xiaoyudeguang/easy-swagger.");
		}
		if (Ops.isEmpty(model.getDescription())) {
			model.setDescription("An useful tool for java developers when using swagger.");
		}
		if (Ops.isEmpty(model.getName())) {
			model.setName(ApplicationUtils.getMainClass().getSimpleName());
		}
		if (model.getScm() == null) {
			model.setScm(new Scm());
		}
	}

	private static boolean handleMaven() throws Exception {
		MavenXpp3Reader reader = new MavenXpp3Reader();
		String myPom = System.getProperty("user.dir") + File.separator + "pom.xml";
		if (new File(myPom).exists()) {
			model = reader.read(new FileReader(myPom));
			return true;
		}
		return false;
	}

	private static boolean handleGradle() throws Exception {
		String path = ApplicationUtils.getMainClass().getProtectionDomain().getCodeSource().getLocation().getPath();
		path = URLDecoder.decode(path, "UTF-8");
		File gradlePropFile = deduceGradlePropPath(path);
		if (gradlePropFile.exists()) {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(gradlePropFile)));
			String line = null;
			while ((line = br.readLine()) != null) {
				line = line.trim();
				setProperty(line);
			}
			br.close();
			return true;
		}
		return false;
	}

	private static void setProperty(String line) {
		String value = line.substring(line.indexOf('=') + 1).trim();
		if (line.startsWith("name")) {
			model.setName(value);
		}
		if (line.startsWith("description")) {
			model.setDescription(value);
		}
		if (line.startsWith("url")) {
			model.setUrl(value);
		}
		if (line.startsWith("version")) {
			model.setDescription(value);
		}
		if (line.startsWith("developers")) {
			List<Developer> developers = Lists.newList();
			String[] developerInfo;
			Developer dev = null;
			for (String developer : line.split(";")) {
				developerInfo = developer.split(":");
				dev = new Developer();
				dev.setName(developerInfo[0]);
				dev.setEmail(developerInfo[1]);
				developers.add(dev);
			}
			model.setDevelopers(developers);
		}
	}

	private static File deduceGradlePropPath(String path) {
		File file = new File(path);
		File gradlePropFile = new File(file, "gradle.properties");
		while (!gradlePropFile.exists()) {
			file = file.getParentFile();
			if (file == null || !file.exists()) {
				break;
			}
			gradlePropFile = new File(file, "gradle.properties");
		}
		return gradlePropFile;
	}
}
